package mes

import (
	"context"
	"encoding/json"
	"ez/apps/mes/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type CheckProcess struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64 `json:"id" bson:"id,omitempty"`

	Name string `json:"name" bson:"name"` //检验工序名称

	Department *Department `json:"department" bson:"department"` //所属车间

	DepartmentId int64 `json:"departmentId" bson:"departmentId"` //所属车间

	Code string `json:"code" bson:"code"` //编码

	CreateAt time.Time  `json:"createAt" bson:"createAt"`
	UpdateAt time.Time  `json:"updateAt" bson:"updateAt"`
	DeleteAt *time.Time `json:"deleteAt" bson:"deleteAt"`
}

func (this *CheckProcess) DocName() string { return "CheckProcess" }

func (this *CheckProcess) GetId() int64 { return this.Id }

func (this *CheckProcess) SetId(id int64) { this.Id = id }

func (this *CheckProcess) Create() error {
	return this.GetFactory().Create(this)
}

func (this *CheckProcess) Replace() error {
	return this.GetFactory().Replace(this)
}

func (this *CheckProcess) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *CheckProcess) Delete() error {
	return this.GetFactory().Delete(this)
}

func (this *CheckProcess) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *CheckProcess) Destroy() error {
	return this.GetFactory().Destroy(this)
}

func (this *CheckProcess) ToString() string {
	return string(this.ToBytes())
}

func (this *CheckProcess) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *CheckProcess) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *CheckProcess) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}

func (this *CheckProcess) LoadDepartment() {
	if this.DepartmentId == 0 {
		return
	}
	this.Department, _ = NewDepartmentCrud().FindId(this.DepartmentId)
}

func (this *CheckProcess) ClearRelationsBeforeSave() mgo.Doc {

	this.Department = nil

	return this
}

func neverUsed_CheckProcess() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type CheckProcessAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *CheckProcess
	Session *ez.Session
}

func NewCheckProcessAccessControl(model *CheckProcess, action string, session *ez.Session) *CheckProcessAccessControl {
	ctrl := &CheckProcessAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.CheckProcessAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
